#!/bin/sh
# Ce script permet de deployer une nouvelle version de Balloon

SVN_CONNECTION="svn+ssh://mosser@cerbere/opt/svn/Balloon"

HTTP_ROOT="/var/www"
WEB_SERVER_SCRIPT="/etc/init.d/apache2"
WEB_USER=www-data

## Ne pas editer sous cette ligne
echo "####### BALLOON UPDATER :: BEGIN ########"

echo "### Gonna stop [`basename $WEB_SERVER_SCRIPT`], just in case of ..."
$WEB_SERVER_SCRIPT stop

echo "### Going to [$HTTP_ROOT]"
cd $HTTP_ROOT

echo "### Saving Old Balloon Directory"
echo "# -> Balloon ==> Balloon-OLD"
if [ -f Balloon-OLD ]
	then
	rm -rf Balloon-OLD
fi
cp -rf Balloon Balloon-OLD

echo "### Getting the new Balloon version"
echo "# -> Deleting the old Balloon directory"
rm -rf Balloon
echo "# Exporting last snapshot from svn"
svn export $SVN_CONNECTION
if [ ! $? -eq 0 ]
	then
		echo "## Error While exporting ..."
		mv Balloon-OLD Balloon
fi

echo "### Setting up Balloon Configuration "
echo "# -> Apache access handler      : [.htaccess]"
cp -rf Balloon-OLD/.htaccess Balloon/.
echo "# -> Paths and Url for Balloon  : [local.conf]"
cp -rf Balloon-OLD/local.conf Balloon/.
echo "# -> Database user and password : [database.conf]"
cp -rf Balloon-OLD/database.conf Balloon/.
echo "# -> TableOfContent generator   : [toc.js]"
cp -rf Balloon-OLD/files/javascript/toc.js Balloon/files/javascript/.
echo "# -> Bajax client library       : [bajax.js]"
cp -rf Balloon-OLD/files/javascript/bajax.js Balloon/files/javascript/.

echo "### Restoring binary data"
echo "# -> Restoring directory contents : [data] "
cp -rf Balloon-OLD/data/* Balloon/data/.
echo "# -> Restoring directory contents : [xmlExportation] "
cp -rf Balloon-OLD/xmlExportation/* Balloon/xmlExportation/.

echo "### Setting up permissions"
echo "# -> group & other ==> ---"
chmod -R go-rwx Balloon
echo "# -> Balloon now belongs to [$WEB_USER]"
chown -R $WEB_USER:$WEB_USER Balloon
echo "# -> update.sh is a schell script ==> r-x"
chmod u+x Balloon/update.sh
echo "### Saving the old version"
TIMESTAMP=`date +"%s"`
echo "# -> Balloon-OLD ==> ~/Balloon-OLD-$TIMESTAMP"
mv Balloon-OLD ~/Balloon-OLD-$TIMESTAMP

echo "### Restarting [`basename $WEB_SERVER_SCRIPT`] ..."
$WEB_SERVER_SCRIPT start
echo "####### BALLOON UPDATER ::  END  ########"
